home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Games Collection 1 / software vault.zip / software vault / CDR10 / YICN23.ZIP / UNITS / ACTOR.CPP next >
C/C++ Source or Header  |  1993-01-17  |  5KB  |  181 lines

  1. #ifndef SCRIPT.H
  2. #include "script.h"
  3. #endif
  4. #ifndef ACTOR.H
  5. #include "actor.h"
  6. #endif
  7.  
  8. animicon * animactor::actorIcons = new animicon[MAX_ICONS];
  9.  
  10. void animactor::loadActor(int position, char far * filename, yakLib * myYakLib)
  11. {
  12.   animactor::actorIcons[position].addAll(filename, icon::normal, myYakLib);
  13. }
  14.  
  15. void animactor::draw(int x, int y, word pagebase)
  16. {
  17.   if (spawnedActor != NULL)
  18.     spawnedActor->draw(x, y, pagebase);
  19.   else
  20.     thisFrame->picture.showMasked(x, y, pagebase);
  21. }
  22.  
  23. void animactor::show(int x, int y, word pagebase)
  24. {
  25.   if (spawnedActor != NULL)
  26.     spawnedActor->show(x, y, pagebase);
  27.   else
  28.     {
  29.     thisFrame->picture.showMasked(x, y, pagebase);
  30.     advance();
  31.     }
  32. }
  33.  
  34.  
  35. void animactor::advance()
  36. {
  37.   lastMapX = mapX;
  38.   lastMapY = mapY;
  39.   if (spawnedActor != NULL)
  40.     spawnedActor->advance();
  41.   else {
  42.   thisFrame = thisFrame->nextFrame;
  43.   if (thisLine != NULL)
  44.   {
  45.     switch (thisLine->command)
  46.     {
  47.       case scriptNode::doNothing    : break;
  48.       case scriptNode::fineMoveRel    : fineMoveRel(thisLine->squareX, thisLine->squareY); break;
  49.     }
  50.     scriptNode * oldLine = thisLine;
  51.     advanceLine();
  52.     if (thisLine == NULL)
  53.       firstLine = NULL;
  54.     delete (oldLine); // free up our script now!
  55.   }}
  56. }
  57.  
  58. void animactor::put(int x, int y)
  59. {
  60.   mapX = x % (int) mymap->width;
  61.   mapY = y % (int) mymap->height;
  62.   if (mymap->mapData[x][y].nextActor == NULL)
  63.     mymap->mapData[x][y].nextActor = this;
  64.   else
  65.     {
  66.     animactor * newpointer = mymap->mapData[x][y].nextActor;
  67.     animactor * oldpointer = NULL;
  68.     while ((newpointer != NULL))
  69.       {
  70.       if (newpointer->squareY >= squareY) //if there's an object past this...
  71.       {
  72.     if (newpointer == mymap->mapData[x][y].nextActor) //if it's the first one
  73.     {
  74.       nextActor = mymap->mapData[x][y].nextActor; //add it to map
  75.       mymap->mapData[x][y].nextActor = this;
  76.       return;
  77.     }
  78.     else // if it's not the first one...
  79.     {
  80.       oldpointer->nextActor = this; //insert this into the list @ the
  81.       nextActor = newpointer;    //appropriate z-point
  82.       return;
  83.     }
  84.       }
  85.       oldpointer = newpointer;
  86.       newpointer = newpointer->nextActor;
  87.       }
  88.     oldpointer->nextActor = this; //or at the end.
  89.     nextActor = NULL;
  90.     }
  91. }
  92.  
  93.  
  94. void animactor::remove(void)
  95. {
  96.   animactor * thisActor = mymap->mapData[mapX][mapY].nextActor;
  97.   if (thisActor == this) // we are the first object in the list
  98.     {
  99.     mymap->mapData[mapX][mapY].nextActor = nextActor; // object after this one
  100.     nextActor = NULL; //so we don't point to the rest of the list
  101.     }
  102.   else
  103.     {
  104.     while (thisActor->nextActor != this)  //find the actor before this one
  105.       thisActor = thisActor->nextActor;
  106.     thisActor->nextActor = nextActor; // skip this image in the list
  107.     nextActor = NULL;
  108.     }
  109.   mymap->mapData[mapX][mapY].refresh = 3;
  110. }
  111.  
  112. void animactor::assignIcon(byte iconNumber, byte identity)
  113. {
  114.   myIconNumber = iconNumber;
  115.   thisFrame = actorIcons[iconNumber].firstFrame;
  116.   if (identity == 255)
  117.     identity = iconNumber;
  118.   myIdentity = identity;
  119. }
  120.  
  121.  
  122. void * animactor::isInSquare(byte searchIdentity)
  123. {
  124.   animactor * thisActor = mymap->mapData[mapX][mapY].nextActor;
  125.   while (thisActor != NULL)
  126.   {
  127.     if ((thisActor->myIdentity == searchIdentity) && (thisActor != this))
  128.       return(thisActor);
  129.     thisActor = thisActor->nextActor;
  130.   }
  131.   return NULL;
  132. }
  133.  
  134. void animactor::moveTo(int x, int y)
  135. {
  136.   remove();
  137.   mapX = (x % mymap->width);
  138.   mapY = (y % mymap->height);
  139.   put(mapX, mapY);
  140. }
  141.  
  142. int animactor::hit(animactor * target)
  143. {
  144.   return(thisFrame->picture.hitXY(mapX*mymap->squareWidth+squareX-thisFrame->picture.width,
  145.          mapY*mymap->squareWidth+squareY - thisFrame->picture.height, &target->thisFrame->picture,
  146.          target->mapX*target->mymap->squareWidth + target->squareX - target->thisFrame->picture.width,
  147.          target->mapY*target->mymap->squareWidth + target->squareY - target->thisFrame->picture.height));
  148. }
  149.  
  150. void animactor::spawn(animactor * spawnactor)
  151. {
  152.   spawnedActor = spawnactor;
  153.   spawnedActor->spawningActor = this;
  154.   spawnedActor->mapX = mapX;
  155.   spawnedActor->mapY = mapY;
  156.   spawnedActor->squareX = squareX;
  157.   spawnedActor->squareY = squareY;
  158.   spawnedActor->mymap = mymap;
  159. //spawnedActor->firstLine = firstLine;
  160. //spawnedActor->lastLine = lastLine;
  161. //spawnedActor->thisLine = thisLine;
  162. }
  163.  
  164. void animactor::fineMoveRel(int x, int y)
  165. {
  166.   remove();
  167.   int totalX = mapX*(int)mymap->squareWidth + (int)squareX + x;
  168.   int totalY = mapY*(int)mymap->squareWidth + (int)squareY + y;
  169.   int totalWidth = (int)mymap->squareWidth * (int)mymap->width;
  170.   int totalHeight = (int)mymap->squareWidth * (int)mymap->height;
  171.   totalX += (totalX < 0) ? (totalWidth) : 0;
  172.   totalY += (totalY < 0) ? (totalHeight) : 0;
  173.   totalX %= totalWidth;
  174.   totalY %= totalHeight;
  175.   squareX = totalX % (int)mymap->squareWidth;
  176.   squareY = totalY % (int)mymap->squareWidth;
  177.   mapX = totalX / (int)mymap->squareWidth;
  178.   mapY = totalY / (int)mymap->squareWidth;
  179.   put(mapX, mapY);
  180. }
  181.